package com.yikes.srb.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.utils.object.BeanUtils;
import com.yikes.srb.leadingModle.form.borrowerattach.BorrowerAttachAddForm;
import com.yikes.srb.leadingModle.form.borrowerattach.BorrowerAttachEditForm;
import com.yikes.srb.leadingModle.form.borrowerattach.BorrowerAttachQuery;
import com.yikes.srb.leadingModle.vo.BorrowerAttachVO;
import com.yikes.srb.pojo.entity.BorrowerAttach;
import com.yikes.srb.mapper.BorrowerAttachMapper;
import com.yikes.srb.service.BorrowerAttachService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 借款人上传资源表 服务实现类
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BorrowerAttachServiceImpl extends ServiceImpl<BorrowerAttachMapper, BorrowerAttach> implements BorrowerAttachService {

    @Override
    public PageResult<BorrowerAttachVO> listPage(BorrowerAttachQuery query) {

        Page<BorrowerAttach> page = lambdaQuery()
                .eq(ObjectUtil.isNotNull(query.getBorrowerId()), BorrowerAttach::getBorrowerId, query.getBorrowerId())
                .like(StrUtil.isNotBlank(query.getImageType()), BorrowerAttach::getImageType, query.getImageType())
                .like(StrUtil.isNotBlank(query.getImageUrl()), BorrowerAttach::getImageUrl, query.getImageUrl())
                .like(StrUtil.isNotBlank(query.getImageName()), BorrowerAttach::getImageName, query.getImageName())
                .page(Page.of(query.getPageNum(), query.getPageSize()));

        List<BorrowerAttachVO> bean = BeanUtils.toBean(page.getRecords(), BorrowerAttachVO.class);
        return PageResult.build(page.getTotal(), page.getPages(), bean);
    }

    @Override
    public BorrowerAttachVO infoId(Long id) {

        BorrowerAttach attach = getById(id);
        BorrowerAttachVO bean = BeanUtils.toBean(attach, BorrowerAttachVO.class);
        return bean;
    }

    @Override
    public void create(BorrowerAttachAddForm form) {

        BorrowerAttach bean = BeanUtils.toBean(form, BorrowerAttach.class);
        save(bean);
    }

    @Override
    public void edit(BorrowerAttachEditForm form) {

        BorrowerAttach bean = BeanUtils.toBean(form, BorrowerAttach.class);
        updateById(bean);
    }

    @Override
    public void del(Long id) {

        baseMapper.deleteById(id);
    }

    @Override
    public List<BorrowerAttachVO> selectBorrowerAttachVOList(Long id) {

        List<BorrowerAttach> borrowerAttachList = lambdaQuery().eq(BorrowerAttach::getBorrowerId, id).list();
        if (CollUtil.isEmpty(borrowerAttachList)) {
            return Collections.emptyList();
        }

        List<BorrowerAttachVO> borrowerAttachVOList = new ArrayList<>();
        borrowerAttachList.forEach(borrowerAttach -> {
            BorrowerAttachVO borrowerAttachVO = new BorrowerAttachVO();
            borrowerAttachVO.setId(borrowerAttach.getId());
            borrowerAttachVO.setBorrowerId(borrowerAttach.getBorrowerId());
            borrowerAttachVO.setImageName(borrowerAttach.getImageName());
            borrowerAttachVO.setImageType(borrowerAttach.getImageType());
            borrowerAttachVO.setImageUrl(borrowerAttach.getImageUrl());

            borrowerAttachVOList.add(borrowerAttachVO);
        });

        return borrowerAttachVOList;
    }
}
